<?php 
include("header.php");
?>
<?php 
function GetArticleMeta($aid) {
  $meta = null;
  if (!($meta = apc_fetch(sprintf('article-meta-%d', $aid)))) {
    $sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.url AS a_url, a.category AS a_category, a.create_time AS a_create_time,
      u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url
      FROM articles AS a, authors AS u WHERE a.author = u.id AND a.id = %d", $aid);
    // print $sql;
    $result = mysql_query($sql);
    $meta = mysql_fetch_array($result);
    if ($meta && $meta['a_title']) {
      apc_add(sprintf('article-meta-%d', $aid), $meta, 3600 * 2);
    }
  }
  return $meta;
}

function PageNav($max_page, $current_page, $cat) {
  $max_dist = 6;
  $i = $current_page - $max_dist;
  if ($i < 1) {
    $i = 1;
  }
  $last_page = $current_page + $max_dist;
  if ($last_page > $max_page) {
    $last_page = $max_page;
  }

  if ($current_page > 1) {
    $prev = $current_page - 1;
    printf('<a class="page-nav" href="/cat%d-%d/?f=prev">上一页</a> ', $cat, $prev);
  }

  for(; $i <= $last_page; ++$i) {
    if($i == $current_page) {
      printf('<a class="page-nav current" href="/cat%d-%d/" title="第%d页">%d</a> ', $cat, $i, $i, $i);
    } else {
      printf('<a class="page-nav" href="/cat%d-%d/" title="第%d页">%d</a> ', $cat, $i, $i, $i);
    }
  }

  if ($current_page < $max_page) {
    $next = $current_page + 1;
    printf('<a class="page-nav" href="/cat%d-%d/?f=next">下一页</a>', $cat, $next);
  }
  printf('<span style="font-size:86%%;color:#a9a;"> (共%d页)</span>', $max_page);
}

$page = intval($_GET['page']);
if ($page == null) {
  $page = 1;
}

$author = intval($_GET['author']);
if ($author == null) {
  $author = 0;
}

$count_per_page = 25;

$conn = mysql_connect("127.0.0.1","root","");
mysql_select_db("xingtan");

$sql=sprintf("SELECT count(a.id) AS total FROM articles AS a, authors AS u WHERE a.author = u.id AND a.author=%d", $author);
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$max_page = ceil($row['total'] / $count_per_page);

$sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.digest AS a_digest, a.create_time AS a_create_time, 
    u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url 
    FROM articles AS a, authors AS u WHERE a.author = u.id AND a.author = %d ORDER BY a.id DESC limit %d", 
    $author, $page * $count_per_page);
$result = mysql_query($sql);

?>

	<div class="main">
		<div class="content">
			<div class="articles">
        <div style="margin:2px 0 4px;font-size:12px;font-weight:bold;"><a href="/page/1/">全部文章</a> 
          <?php 
            printf(' &gt; <a href="/cat%d/">%s</a>', $category, $category_map[$category]['name']);
          ?> 
        </div>
        <?php 
          PageNav($max_page, $page, $category);
          $index = 0;
          while($row = mysql_fetch_array($result)) {
            if($index >= ($page - 1) * $count_per_page) {
              printf('<div>');
              if (false && $row['u_id'] > 0) {
                printf('<div style="float:right;font-size:12px;line-height:22px;margin-right:8px;">'.
                  '<a target="_blank" title="%s" href="%s">%s</a></div>', $row['u_title'], $row['u_url'], $row['u_name']);
              }
              printf('<h3 class="article-title"><a target="_blank" title="%s" href="/a/%d.html">%s</a></h3>', 
                  $row['a_create_time'], $row['a_id'], $row['a_title']);
              printf('</div>');
            }
            ++$index;
          }

          print('<hr/>');
          PageNav($max_page, $page, $category);
        ?>
			</div>
		  <div class="seperator"></div>
		</div>
		<div class="sources">
      <div class="bar-title"><span>精华推荐</span> <a class="more" target="_blank" href="/essence/1/">更多...</a></div>
      <ul>
        <?php
            $sql=sprintf("SELECT id FROM articles WHERE category >= 0 AND level > 20 ORDER BY id DESC LIMIT 10;");
            $esseces = mysql_query($sql);
            while($row = mysql_fetch_array($esseces)) {
              $article_meta = GetArticleMeta($row['id']);
              if ($article_meta) {
                printf('<div class="icon-disc"></div><li><a target="_blank" href="/a/%d.html?f=essence">%s</a></li>', 
                    $row['id'], $article_meta['a_title']);
              }
           }
        ?>
      </ul>
    </div>
		</div>
	</div>

<?php 
mysql_close($conn);
include("footer.php");
?>
